package com.alingrad.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.GridLayoutAnimationController;

import com.alingrad.R;

public class GridLayout extends ViewGroup {
	private int mAmountWidth;
	private int mAmountHeight;
	
	private int mUnitWidth;
	private int mUnitHeight;
	
	public GridLayout(Context context) {
		this(context, null, 0);
	}

	public GridLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public GridLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		TypedArray a = getContext().obtainStyledAttributes(attrs, 
				R.styleable.GridLayout);
		mAmountWidth = a.getInt(R.styleable.GridLayout_amount_width, 1);
		mAmountHeight = a.getInt(R.styleable.GridLayout_amount_height, 1);
		a.recycle();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        final int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec);

        final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        final int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
        
        if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
        	throw new RuntimeException("GridLayout cannot have UNSPECIFIED dimensions");
        }
        
        final int width = widthSpecSize - getPaddingLeft() - getPaddingRight();
        final int height = heightSpecSize - getPaddingTop() - getPaddingBottom();
        
        final int unitWidth = mUnitWidth = width / mAmountWidth;
        final int unitHeight = mUnitHeight = height / mAmountHeight;
        
        final int count = getChildCount();
        
        for (int i = 0; i < count; i++) {
        	final View child = getChildAt(i);
        	
        	int childWidthSpec = MeasureSpec.makeMeasureSpec(unitWidth, MeasureSpec.EXACTLY);
        	int childHeightSpec = MeasureSpec.makeMeasureSpec(unitHeight, MeasureSpec.EXACTLY);
        	
        	child.measure(childWidthSpec, childHeightSpec);
        }
        
        setMeasuredDimension(widthSpecSize, heightSpecSize);
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		final int columns = mAmountWidth;
        final int paddingLeft = getPaddingLeft();
        final int paddingTop = getPaddingTop();
        final int columnWidth = mUnitWidth;
        final int rowHeight = mUnitHeight;
        final int count = getChildCount();

        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final int column = i % columns;
                final int row = i / columns;

                int childLeft = paddingLeft + column * columnWidth;
                int childTop = paddingTop + row * rowHeight;

                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            }
        }
	}
	
	@Override
	protected void attachLayoutAnimationParameters(View child,
			LayoutParams params, int index, int count) {
		
		GridLayoutAnimationController.AnimationParameters animationParams =
            (GridLayoutAnimationController.AnimationParameters)
                    params.layoutAnimationParameters;

	    if (animationParams == null) {
	        animationParams = new GridLayoutAnimationController.AnimationParameters();
	        params.layoutAnimationParameters = animationParams;
	    }
	
	    animationParams.count = count;
	    animationParams.index = index;
	    animationParams.columnsCount = mAmountWidth;
	    animationParams.rowsCount = mAmountHeight;
	
	    animationParams.column = index % mAmountWidth;
	    animationParams.row = index / mAmountWidth;
	}
	
}
